package com.dianrui.agent.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.bean.Planning;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PlanningAdapter extends BaseAdapter {

    Context context;
    List<Planning> chargeList;
    PlanningListener planningListener;

    public PlanningAdapter(Context context, List<Planning> chargeList) {
        this.context = context;
        this.chargeList = chargeList;
    }

    public void refresh(List<Planning> chargeList) {
        this.chargeList = chargeList;
        this.notifyDataSetChanged();
    }

    public interface PlanningListener {
        void clickMain(String admin_id);
    }

    public void setOnPlanningListener(PlanningListener planningListener) {
        this.planningListener = planningListener;
    }


    @Override
    public int getCount() {
        return chargeList.size();
    }

    @Override
    public Object getItem(int position) {
        return chargeList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_planning, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (chargeList != null && chargeList.size() > 0) {
            final Planning planning = chargeList.get(position);
            holder.nickname.setText(planning.nickname);
            holder.dispatchMonthEffective.setText( planning.dispatch_month_effective );
            holder.dispatchMonthInvalid.setText( planning.dispatch_month_invalid );
            holder.dispatchDayEffective.setText(planning.dispatch_day_effective);
            holder.dispatchDayInvalid.setText(planning.dispatch_day_invalid);
            holder.more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (planningListener != null) {
                        planningListener.clickMain(planning.admin_id);
                    }
                }
            });
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (planningListener != null) {
                        planningListener.clickMain(planning.admin_id);
                    }

                }
            });
        }


        return convertView;
    }


     class ViewHolder {
        @BindView(R.id.nickname)
        TextView nickname;
        @BindView(R.id.more)
        TextView more;
        @BindView(R.id.dispatch_month_effective)
        TextView dispatchMonthEffective;
        @BindView(R.id.dispatch_month_invalid)
        TextView dispatchMonthInvalid;
        @BindView(R.id.dispatch_day_effective)
        TextView dispatchDayEffective;
        @BindView(R.id.dispatch_day_invalid)
        TextView dispatchDayInvalid;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
